<template>
  <div class="box brandlist">
    <header class="header">
      <van-nav-bar
        :title="column + '-' + brand"
        left-arrow
        @click-left="$router.back()"
      />
    </header>
    <div class="content" ref='cont' @scroll="toTop($event)">
      <van-dropdown-menu>
        <van-dropdown-item v-model="value" :options="option" />
        <van-dropdown-item title="筛选" ref="item">
          <van-cell center title="请输入价格区间">
            <input type="text" class="min" v-model="min" />
            ~
            <input type="text" class="max" v-model="max" />
          </van-cell>
          <div style="padding: 5px 16px;">
            <van-button type="danger" block round @click="onConfirm">
              确认
            </van-button>
          </div>
        </van-dropdown-item>
      </van-dropdown-menu>
      <van-card
        v-for="item in list"
        :key="item.proId"
        :num="item.stock"
        :price="item.price"
        :desc="item.introduce"
        :title="item.proName"
        :thumb="item.proImg"
        @click="fn(item.proId)"
      />
      <van-icon name="back-top" class="toTop" v-show="flag" @click="gotoTop" />
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, DropdownMenu, DropdownItem, Icon, Toast, Button, Card } from 'vant'

import { getColumnBrandList } from '../../api/index.js'

Vue.use(NavBar)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Icon)
Vue.use(Toast)
Vue.use(Button)
Vue.use(Card)
export default {
  data () {
    return {
      column: this.$route.query.column,
      brand: this.$route.query.brand,
      value: 0,
      option: [
        { text: '价格排序', value: 0 },
        { text: '价格升序', value: 1 },
        { text: '价格降序', value: 2 }
      ],
      min: null,
      max: null,
      list: [],
      searchList: [],
      flag: false
    }
  },
  methods: {
    onConfirm () {
      this.$refs.item.toggle()
      // 深拷贝
      this.list = this.searchList.map(val => val)
      if ((!this.min || !this.max) && this.min !== 0 && this.max !== 0) {
        Toast('缺少筛选范围')
      } else {
        this.list = this.list.filter(val => {
          return val.price <= Math.max(this.min, this.max) && val.price >= Math.min(this.min, this.max)
        })
      }
    },
    // 回到顶部
    gotoTop () {
      this.$refs.cont.scrollTop = 0
    },
    fn (proId) {
      this.$router.push('/detail/' + proId)
    },
    toTop (event) {
      if (event.target.scrollTop > 500) {
        this.flag = true
      } else {
        this.flag = false
      }
    }
  },
  mounted () {
    getColumnBrandList({
      column: this.column,
      brand: this.brand
    }).then(res => {
      // console.log(res.data.data)
      // 先把数据存进searchList中
      this.searchList = res.data.data
      // 修改的话,深拷贝一份存入list中
      this.list = JSON.parse(JSON.stringify(res.data.data))
    })
  },
  watch: {
    value (a) {
      if (a === 1) {
        this.list.sort((a, b) => {
          return a.price - b.price
        })
      } else if (a === 2) {
        this.list.sort((a, b) => {
          return b.price - a.price
        })
      } else {
        this.list = this.searchList
      }
    }
  }
}
</script>

<style lang="scss">
  .brandlist {
    .toTop {
      position: fixed;
      bottom: 1rem;
      right: 0.4rem;
      font-size: 0.3rem;
      background: rgba(200,190,200,0.5);
      border: 1px solid yellow;
      border-radius: 6px;
      padding: 0.1rem;
    }
    .min,.max {
      width: 0.5rem;
    }
  }
</style>
